//
//  NavigationViewController.m
//  Comma
//
//  Created by 刘轩博 on 15/2/3.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NavigationViewController.h"
#import "YRSideViewController.h"
#import "LeftViewController.h"
#import "MyDynamicViewController.h"
#import "PersonalCenterViewController.h"
#import "HelpViewController.h"
#import "SettingsViewController.h"
#import "NDetailListViewController.h"
@interface NavigationViewController ()<HelpViewControllerDelegate>

@end

@implementation NavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createAllViewController];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeRootController:) name:@"PUSH" object:nil];
    
    
    __weak typeof (self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - 创建controller
- (void)createAllViewController
{
    self.controllerArray = [NSMutableArray array];
    
    self.rootController = (NDetailListViewController *)self.viewControllers.firstObject;
    
    MyDynamicViewController *dynamic = [[MyDynamicViewController alloc] init];
    PersonalCenterViewController *person = [[PersonalCenterViewController alloc] init];
    HelpViewController *help = [[HelpViewController alloc] init];
    SettingsViewController *set = [[SettingsViewController alloc] init];
    [self.controllerArray addObject:dynamic];   
    [self.controllerArray addObject:person];    
    [self.controllerArray addObject:help];
    [self.controllerArray addObject:set];
    
}

- (void)changeRootController:(NSNotification *)notification
{
    
    YRSideViewController * sideview = [YRSideViewController share];
    if ([[notification.userInfo objectForKey:@"text"] isEqualToString:@"首页"]) {
        // 判断是否是当前视图控制器
        UIViewController *view = self.viewControllers.firstObject;
        if (![view isKindOfClass:[NDetailListViewController class]]) {
//            NSLog(@"%@",@[_rootController]);
            [self setViewControllers:@[_rootController] animated:NO];

        }
    } else if ([[notification.userInfo objectForKey:@"text"] isEqualToString:@"我的动态"]) {
        
        MyDynamicViewController *my = [self.controllerArray objectAtIndex:0];
        [self setViewControllers:@[my] animated:NO];
    } else if ([[notification.userInfo objectForKey:@"text"] isEqualToString:@"个人中心"]) {
        PersonalCenterViewController *personal = [self.controllerArray objectAtIndex:1];
        [self setViewControllers:@[personal] animated:NO];
    } else if ([[notification.userInfo objectForKey:@"text"] isEqualToString:@"帮助"]) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"right", @"move", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LINEA" object:nil userInfo:dic];
        [UIView animateWithDuration:0.3 animations:^{
            [sideview.rootViewController.view setFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        }];
        HelpViewController *help = [self.controllerArray objectAtIndex:2];
        help.delegate = self;
        
        [self presentViewController:help animated:YES completion:nil];
        
    } else if ([[notification.userInfo objectForKey:@"text"] isEqualToString:@"设置"]){
        SettingsViewController *set = [self.controllerArray objectAtIndex:3];
        [self setViewControllers:@[set] animated:NO];
    }
    
    if (![[notification.userInfo objectForKey:@"text"] isEqualToString:@"帮助"]) {
        [sideview.coverButton removeFromSuperview];
        [UIView animateWithDuration:0.45 animations:^{
            [sideview.rootViewController.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        }];
    }
    
}

- (void)helpControlMoveToOrigin
{
    YRSideViewController * sideview = [YRSideViewController share];
    [UIView animateWithDuration:0.3 animations:^{
        sideview.rootViewController.view.frame = CGRectMake(SCREEN_WIDTH / 3 * 2, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
